/*
 * 文件名：PostController.java
 * 版权：Copyright 2007-2015 517na Tech. Co. Ltd. All Rights Reserved. 
 * 描述： PostController.java
 * 修改人：yanshu
 * 修改时间：2015年12月21日
 * 修改内容：新增
 */
package com.feichuang.chineseWoodCarving.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.better517na.commons.Response;
import com.better517na.commons.StatusCode;
import com.feichuang.chineseWoodCarving.business.IPostBusiness;
import com.feichuang.chineseWoodCarving.model.MPagerRequestVo;
import com.feichuang.chineseWoodCarving.model.MPagerResultVo;
import com.feichuang.chineseWoodCarving.model.MPost;
import com.feichuang.chineseWoodCarving.model.MUser;
import com.google.gson.Gson;

/**
 * TODO 帖子操作，包括发帖子，帖子展示.
 * <p>
 * TODO 详细描述
 * <p>
 * TODO 示例代码
 * 
 * <pre>
 * </pre>
 * 
 * @author yanshu
 */
@Controller
@RequestMapping(value = "/PostHadle")
public class PostController {

    /**
     * 添加字段注释.
     */
    @Resource(name = "postBusiness")
    private IPostBusiness postBusiness;

    /**
     * 
     * TODO 添加供求信息、帖子信息.
     * 
     * @param request
     *            请求
     * @param response
     *            响应
     * @param param
     *            请求json
     * @throws IOException
     */
    @RequestMapping(value = "/addPost", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String insetPost(HttpServletRequest request, HttpServletResponse response, String param, String type) throws IOException {
        Gson gson = new Gson();
        Response<String> res = new Response<String>();
        MUser userInfo = (MUser) request.getSession().getAttribute("LoginStaff");
        if (null == userInfo) {
            res.setCode(StatusCode.BAD_REQUEST);
            res.setMessage("请登录后才能使用此功能");
        } else {
            MPost post = gson.fromJson(param, MPost.class);
            post.setUserId(userInfo.getUserNo());
            post.setUserName(userInfo.getUserName());
            post.setUserType(userInfo.getUserType());
            if (postBusiness.insertPost(post)) {
                res.equals(StatusCode.SUCCESS);
                res.setMessage("发帖成功");
            } else {
                res.equals(StatusCode.ERROR);
                res.setMessage("风速过大");
            }
        }
        return gson.toJson(res);
    }

    /**
     * 
     * TODO 查询供求信息.
     * 
     * @param request
     *            请求
     * @param response
     *            响应
     * @param param
     *            请求json
     * @throws IOException
     */
    @RequestMapping(value = "/postQuerForPage", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String pageQueryPost(HttpServletRequest request, HttpServletResponse response, String param, String type) throws IOException {
        Gson gson = new Gson();
        Response<MPagerResultVo<?>> res = new Response<MPagerResultVo<?>>();
        try {
            MPagerRequestVo<MPost> pageRequest = gson.fromJson(param, MPagerRequestVo.class);
            MPagerResultVo<MPost> pageResult = postBusiness.selectPostForPage(pageRequest);
            res.setContent(pageResult);
            res.setCode(StatusCode.SUCCESS);
            res.setMessage("成功");
        } catch (Exception e) {
            res.equals(StatusCode.ERROR);
            res.setMessage("风速过大");
        }
        return gson.toJson(res);
    }

    /**
     * 
     * TODO 首页.
     * 
     * @param request
     *            请求
     * @param response
     *            响应
     * @param param
     *            请求json
     * @throws IOException
     */
    @RequestMapping(value = "/postQuerForPageUser", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String pageQueryPostforUser(HttpServletRequest request, HttpServletResponse response, String param, int type) throws IOException {
        Gson gson = new Gson();
        Response<MPagerResultVo<?>> res = new Response<MPagerResultVo<?>>();
        try {
            MPagerRequestVo<MPost> pageRequest = gson.fromJson(param, MPagerRequestVo.class);
            pageRequest.getDataModel().setPostType(2);
            MPagerResultVo<MPost> pageResult = postBusiness.selectPostForPage(pageRequest);
            res.setContent(pageResult);
            res.setCode(StatusCode.SUCCESS);
            res.setMessage("成功");
        } catch (Exception e) {
            res.equals(StatusCode.ERROR);
            res.setMessage("风速过大");
        }
        return gson.toJson(res);
    }

}
